<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Dvideo extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'v_type' => 2
            ];
            if(isset($user_name) && $user_name != ''){
                $where['v_title|v_top_title|user_d_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($s_pp_id) && $s_pp_id != ''){
                $where['v_pp_id'] = $s_pp_id;
            }
            if(isset($status) && $status != ''){
                $where['v_status'] = $status;
            }
            if(isset($v_top) && $v_top != ''){
                $where['v_top'] = $v_top;
            }
            if(isset($v_info_top) && $v_info_top != ''){
                $where['v_info_top'] = $v_info_top;
            }
            if(session('agid') == 10){
                $where['v_pp_id'] = session('uid');
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_pp_video')->alias('v')
                ->join('up_u_user u','v.v_pp_id=u.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $field = 'v.*,user_d_name';
            $lists = Db::table('up_pp_video')->alias('v')
                ->join('up_u_user u','v.v_pp_id=u.user_id','LEFT')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('v_status asc,v_sort desc,v_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_phone')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function newAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            if(session('agid') == 10){
                $v_pp_id = session('uid');
            }
            if(!$v_url){
                return json(['code' => 100, 'msg' => '请上传视频']);
            }else{
                $data = [];
                $urls = explode(',',$v_url);
                foreach ($urls as $k => $v){
                    $son = [
                        'v_title' => $v_title,
                        'v_top_title' => $v_top_title,
                        'v_type' => 2,
                        'v_pp_id' => $v_pp_id,
                        'v_url' => $v,
                        'v_time' => date('Y-m-d H:i:s')
                    ];
                    if(session('agid') != 10){
                        $son['v_sort'] = $v_sort;
                        $son['v_top'] = $v_top;
                        $son['v_info_top'] = $v_info_top;
                    }
                    $data[] = $son;
                }
            }
            $res = Db::table('up_pp_video')->insertAll($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_phone')
            ->order('user_sort desc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('dvideo/newadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function newEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            if(session('agid') == 10){
                $v_pp_id = session('uid');
            }
            $data = [
                'v_title' => $v_title,
                'v_top_title' => $v_top_title,
                'v_pp_id' => $v_pp_id,
                'v_url' => $v_url,
                'v_time' => date('Y-m-d H:i:s')
            ];
            if(session('agid') != 10){
                $data['v_sort'] = $v_sort;
                $data['v_top'] = $v_top;
                $data['v_info_top'] = $v_info_top;
            }
            $res = Db::table('up_pp_video')->where('v_id',$v_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $v_id = input('v_id');
        $info = Db::name('pp_video')
            ->where('v_id',$v_id)
            ->find();
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_phone')
            ->order('user_sort desc')
            ->select();
        $this->assign('pps',$pps);
        $this->assign('info',$info);
        return $this->fetch('dvideo/newedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function newDel(){
        $new_id = input('id');
        $res = Db::table('up_pp_video')->where('v_id',$new_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 状态
     * @return \think\response\Json
     */
    public function changeStatus(){
        $id = input('id');
        $param = [
            'v_status'=>input('num'),
        ];
        $res = Db::name('pp_video')->where('v_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 状态
     * @return \think\response\Json
     */
    public function changeTop(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权修改排序顺序']);
        }
        $id = input('id');
        $param = [
            'v_top'=>input('num'),
        ];
        $res = Db::name('pp_video')->where('v_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权修改排序顺序']);
        }
        $id = input('id');
        $param = [
            'v_sort'=>input('value'),
        ];
        $res = Db::table('up_pp_video')->where('v_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function changeMainStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权修改排序顺序']);
        }
        $id = input('id');
        $param = [
            'v_info_top'=>input('num'),
        ];
        $res = Db::table('up_pp_video')->where('v_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}